home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
VideoToolbox 96.06.15
/
(Demos)
/
TimeCPU.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-19
|
31KB
|
1,197 lines
/*
TimeCPU.c
Denis G. Pelli, 1991-1995
This routine uses my Timer.c to measure the timing of basic CPU
operations and several random number generators. (Apple's Microseconds() routine
would do just as well, instead of Timer.c, but wasn't available when I wrote this.)
The timing seems to be very accurate. It ought to be as accurate as the
frequency of the oscillator in the VIA chip. However, I haven't checked the
timing against a known standard.
The Fixed data type is predefined by Apple as a long (i.e. 32 bits) with an assumed
decimal point in the middle.
The access to video memory overwrites a small part of your main screen, the
first 40 bytes. This will be in the upper left hand corner of your display, and
will usually be barely noticeable. I like seeing that, as it confirms that the
program really is accessing the video memory.
This program requests a memory partition of 900K. It will run in less, but will
then do fewer iterations.
NOTE: This file is part of the VideoToolbox archive of C sources for the Mac.
You can download the whole VideoToolbox from any Info-Mac mirror:
ftp://mirror.apple.com/mirrors/info-mac/dev/lib/video-toolbox-95-11-10-c.hqx
ftp://mirrors.aol.com/pub/info-mac/dev/lib/video-toolbox-95-11-10-c.hqx
The glue libraries NameRegistryLib and DriverServicesLib are in the VideoToolbox Libs folder.
HISTORY:
1/91 dgp wrote it
2/16/91 dgp added fpu test, to fail gracefully if compiled with FPU support, but FPU
is not present.
3/4/91 dgp added timing of random number generators
8/6/91 dgp added timing of RandFill.
8/24/91 dgp Made compatible with THINK C 5.0.
1/25/92 dgp Calibrate and correct for the slowness of TimeIt().
Measure and subtract off the small loop overhead.
Identify machine and compiler.
Automatically append results to TimeCPU.data file.
1/29/92 dgp Time move from memory to video memory, for showing movies.
Added transcendental functions since Radius 8881 init and System 7.01
speed them up dramatically and the Quadra is reputed to
do them very slowly.
1/30/92 dgp Access video memory only in 32-bit mode, to avoid crashes.
8/19/92 dgp time the 68881 instructions _sin, _sqrt, _exp, _log
Use new Timer.c instead of old TimeIt.c
8/28/92 dgp updated to use new reentrant Timer.c
11/18/92 dgp renamed output file to “TimeCPU results”
1/11/93 dgp check for presence of 68020. Put Gestalt tests in main, without
any fpu usage, since program was crashing when fpu was absent
before getting to the fpu test. (Supposedly that was fixed
in THINK C 5. Oh well.)
2/7/93 dgp added timing of SetPixelsQuickly().
7/9/93 dgp check for 32-bit addressing capability.
3/13/94 dgp added timing of short arithmetic, and put conditionals around each section.
6/1/94 dgp added timing of BlockMoveData().
6/14/94 dgp can32 is now computed by calling TrapAvailable(_SwapMMUMode), which
returns the correct answer even on Macs with dirty ROMs.
7/31/94 dgp made compatible with new SANE.h in Universal Headers.
9/5/94 dgp removed assumption in printf's that int==short.
10/2/94 dgp deleted RandomX() since it's part of SANE, which doesn't exist on PowerPC,
and, in any event, was uselessly slow on 68k machines. Tidied up the
printout for readability even for computers as fast as the PowerPC.
4/9/95 dgp added a few tests relevant to nrand(). Increased n. Polished the printout a bit.
4/11/95 dgp changed declaration of bufferHandle from void ** to Handle, for compatibility with
old pre-universal apple headers.
5/23/95 dgp Apple changed the prototype in the header file from SwapMMUMode(char *) to
SwapMMUMode(signed char *). To retain compatibility with both old and new
headers, I cast the argument (void *).
11/20/95 dgp added timing of moves using float pointers, since they're supposed to be fast on the PowerPCs.
1/28/96 dgp only call BlockMoveDataUncached on PCI Macs; otherwise use BlockMoveData.
3/4/96 dgp made compatible with THINK C 7.
*/
#include "VideoToolbox.h"
#ifndef __TRAPS__
#include <Traps.h> // _SwapMMUMode
#endif
//#include <Menus.h> // DrawMenuBar
void ShrinkRect(Rect *r,int hDivisor,int vDivisor);
void ExpandRect(Rect *r,double hMag,double vMag);
void ExpandAndOffsetRect(Rect *r,double hMag,double vMag,double hOffset,double vOffset);
void TimeCPU(void);
#if GENERATINGPOWERPC
// BlockMoveDataUncached is only available on PowerPC. Apparently video buffers are "uncacheable" and
// BlockMoveData uses an instruction that is emulated slowly in that case.
extern void BlockCopy(const void *srcPtr, void *destPtr, Size byteCount);
extern void BlockMoveDataUncached(const void *srcPtr, void *destPtr, Size byteCount);
#else
#define BlockCopy BlockMoveData
#define BlockMoveDataUncached BlockMoveData
#endif
#if UNIVERSAL_HEADERS>1
#ifndef __CODEFRAGMENTS__
#include <CodeFragments.h>
#endif
#else
#undef NO_DRIVER_SERVICES_LIB
#define NO_DRIVER_SERVICES_LIB 1
#endif
void main(void)
{
long value;
Require(gestaltOriginalQD);
Gestalt(gestaltTimeMgrVersion,&value);
if(value<gestaltRevisedTimeMgr)
PrintfExit("Sorry, your System is too old; I need at least \n"
"the Revised Time Manager.\n");
TimeCPU();
}
void TimeCPU(void)
{
long n,quickDraw;
short i;
register long *paL,*pbL,iL,jL,kL,mL;
double x,y,z;
Fixed xF,yF,zF;
Handle bufferHandle;
void *buffer,*buffer2;
char Buffer[32];
FILE *o[2],*dataFile;
GDHandle device;
double s,s0,overhead;
Timer *timer;
OSErr osErr,error;
long value;
Boolean can32;
Boolean pci; // Is this a PCI Mac? Only the PCI Macs have BlockMoveDataUncached.
MaximizeConsoleHeight();
/* INITIALIZE QuickDraw */
#if (THINK_C || THINK_CPLUS || SYMANTEC_C)
console_options.ncols = 90;
#elif __MWERKS__
SIOUXSettings.columns=90;
#elif
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitCursor();
#endif
printf("Welcome to TimeCPU.\n");
assert(StackSpace()>4000);
can32=TrapAvailable(_SwapMMUMode);
timer=NewTimer();
#define gestaltNameRegistryVersion 'nreg' // support old versions of Gestalt.h
error=Gestalt(gestaltNameRegistryVersion,&value);
pci=(error==0); // are there PCI slots?
#if CFMSYSTEMCALLS
if(GENERATINGPOWERPC && pci){
// The BlockMoveDataUncached glue is in a shared library.
// If the DriverServicesLib is weak-linked, and missing, we need to
// check for the library, to prevent a crash if we try to access the
// library's exports.
CFragConnectionID connID;
Ptr mainAddr;
Str255 errName;
long templong;
int error;
error=Gestalt(gestaltCFMAttr,&templong); // Code Fragment Manager?
if(!error){
error=GetSharedLibrary((ConstStr63Param)"\pDriverServicesLib"
,kAnyCFragArch,kFindCFrag,&connID,&mainAddr,errName);
if(error)PrintfExit("\nCouldn't load %#s. GetSharedLibrary error=%d\n\007",errName,(int)error);
}
}
#endif
o[0]=stdout;
o[1]=dataFile=fopen("TimeCPU results","a"); /* Append to data file */
if(dataFile!=NULL){
printf("Key results will be appended to “TimeCPU results” file.\n\n");
SetFileInfo("TimeCPU results",'TEXT','ttxt');
}
else printf("Could not open “TimeCPU results” file\n\n");
ffprintf(o,"\n%s\n",BreakLines(IdentifyMachine(),80));
ffprintf(o,"%s\n\n",BreakLines(IdentifyCompiler(),80));
ffprintf(o," Time Operation\n");
srand(clock());
y=sqrt(2.0);
z=sqrt(3.0);
kL=y*1000.;
mL=z*10.;
RandFill(Buffer,sizeof(Buffer));
n=100000; // REDUCE THIS NUMBER TO SPEED UP THE TESTING, BUT REDUCE PRECISION.
overhead=0.0;
StartTimer(timer);
for(iL=n/10;iL>0;iL--) ;
overhead=StopTimerSecs(timer)/n-overhead; // the loop overhead per operation
// Time various ways of moving memory
for(;n>0;){
bufferHandle=NewHandle(2*sizeof(long)*n);
if(bufferHandle==NULL)bufferHandle=TempNewHandle(2*sizeof(long)*n,&osErr);
if(bufferHandle!=NULL)break;
n/=2;
printf("Reducing iterations to %ld to fit in available memory.\n",n);
}
assert(bufferHandle!=NULL);
HLockHi(bufferHandle);
buffer=*bufferHandle;
buffer=(void *)(((unsigned long)buffer+15) & ~15UL); // round up to multiple of 16
buffer2=(long *)buffer+n;
buffer2=(void *)((unsigned long)buffer2 & ~15UL); // round down to multiple of 16
{
StartTimer(timer);
BlockMove(buffer2,buffer,4*n);
s=StopTimerSecs(timer);
ffprintf(o,"%11.3f ms BlockMove(,,%ld); // %.1f MB/s\n"
,s*1e3,n*4,n*4/1024./1024./s);
}
{
StartTimer(timer);
BlockMoveData(buffer2,buffer,4*n);
s=StopTimerSecs(timer);
ffprintf(o,"%11.3f ms BlockMoveData(,,%ld); // %.1f MB/s\n"
,s*1e3,n*4,n*4/1024./1024./s);
}
{
register double *paF,*pbF;
n/=sizeof(*paF)/4;
paF=(void *)buffer;
pbF=(void *)buffer2;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs *paF++=*pbF++; // double, memory to memory, %.1f MB/s\n",s*1e6,sizeof(*paF)*1e-6/s);
n*=sizeof(*paF)/4;
}
{
register double *paF,*pbF;
unsigned long bytes=sizeof(*paF);
n/=sizeof(*paF)/4;
paF=(void *)buffer;
pbF=(void *)buffer2;
paF--;pbF--;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs *++paF=*++pbF; // double, memory to memory, %.1f MB/s\n",s*1e6,sizeof(*paF)*1e-6/s);
n*=sizeof(*paF)/4;
}
{
register float *paF,*pbF;
n/=sizeof(*paF)/4;
paF=(float *)buffer;
pbF=(float *)buffer2;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
*paF++=*pbF++;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs *paF++=*pbF++; // float, memory to memory, %.1f MB/s\n",s*1e6,sizeof(*paF)*1e-6/s);
n*=sizeof(*paF)/4;
}
{
register float *paF,*pbF;
n/=sizeof(*paF)/4;
paF=(void *)buffer;
pbF=(void *)buffer2;
paF--;pbF--;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
*++paF=*++pbF;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs *++paF=*++pbF; // float, memory to memory, %.1f MB/s\n",s*1e6,sizeof(*paF)*1e-6/s);
n*=sizeof(*paF)/4;
}
{
paL=(void *)buffer;
pbL=(void *)buffer2;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs *paL++=*pbL++; // long, memory to memory, %.1f MB/s\n",s*1e6,sizeof(*paL)*1e-6/s);
}
Gestalt(gestaltQuickdrawVersion,&quickDraw);
if(quickDraw>=gestalt8BitQD){
device=GetMainDevice();
if(device!=NULL){
paL=(long *)(**(**device).gdPMap).baseAddr;
if(paL!=NULL){
signed char mode=true32b;
long *pSave=paL;
StartTimer(timer);
if(can32)SwapMMUMode((void *)&mode);
for(iL=n/10;iL>0;iL--) paL-=10;
if(can32)SwapMMUMode((void *)&mode);
s0=StopTimerSecs(timer)/n-overhead;
paL=pSave;
pbL=(long *)buffer2;
StartTimer(timer);
if(can32)SwapMMUMode((void *)&mode);
for(iL=n/10;iL>0;iL--){
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
*paL++=*pbL++;
paL-=10;
}
if(can32)SwapMMUMode((void *)&mode);
s=StopTimerSecs(timer)/n-overhead;
s-=s0; // remove time for the paL-=10;
ffprintf(o,"%11.3f µs *paL++=*pbL++; // long, memory to video mem. %.1f MB/s\n",s*1e6,4e-6/s);
{
Boolean tryIt=1;
char *blockMoveName;
//if(pci)tryIt=Choose(tryIt,"\nTry using BlockMoveDataUncached to copy from memory to video buffer?\n",noYes,2);
//else tryIt=Choose(tryIt,"\nTry using BlockMoveData to copy from memory to video buffer?\n",noYes,2);
if(tryIt){
// this fails on some Macs
if(pci)blockMoveName="BlockMoveDataUncached";
else blockMoveName="BlockMoveData";
paL=pSave;
pbL=(long *)buffer2;
BlockMoveData(paL,pbL,4*n); // first copy from screen to memory, so we won't mess up screen
StartTimer(timer);
if(pci)BlockMoveDataUncached(pbL,paL,4*n);
else BlockMoveData(pbL,paL,4*n);
s=StopTimerSecs(timer)-overhead;
ffprintf(o,"%11.3f ms %s(,,%ld); // mem.to vid.mem. %.1f MB/s\n"
,s*1e3,blockMoveName,n*4,n*4/1024./1024./s);
}
if(tryIt && pci){
// this fails on some Macs
blockMoveName="BlockCopy";
paL=pSave;
pbL=(long *)buffer2;
BlockMoveData(paL,pbL,4*n); // first copy from screen to memory, so we won't mess up screen
StartTimer(timer);
BlockCopy(pbL,paL,4*n);
s=StopTimerSecs(timer)-overhead;
ffprintf(o,"%11.3f ms %s(,,%ld); // mem.to vid.mem. %.1f MB/s\n"
,s*1e3,blockMoveName,n*4,n*4/1024./1024./s);
}
}
}
}
}
DisposeHandle(bufferHandle);
buffer=buffer2=NULL;
// Time the VideoToolbox routines that copy images (based on CopyBits and CopyBitsQuickly).
Gestalt(gestaltQuickdrawVersion,&quickDraw);
if(quickDraw>=gestalt8BitQD){
unsigned long row[256],row2[256];
int rowLength=256,clutSize;
assert(StackSpace()>4000);
n/=100;
device=GetMainDevice();
clutSize=GDClutSize(device);
for(i=0;i<rowLength;i++)row[i]=nrand(clutSize);
StartTimer(timer);
for(iL=n;iL>0;iL--){
SetDevicePixelsQuickly(device,0,0,row,rowLength);
}
// NOTE: if you single step through this in the Debugger
// you'll get an apparent read-back error if the Debugger
// redraws the Menu bar between writing and reading the pixels.
s=StopTimerSecs(timer)/n-overhead;
GetDevicePixelsQuickly(device,0,0,row2,rowLength);
ffprintf(o,"%11.3f ms SetPixelsQuickly(,,,,%d);// %d-bit pixels, %.3f MB/s\n",s*1e3
,(int)rowLength,(int)(**(**device).gdPMap).pixelSize
,(double)rowLength*(**(**device).gdPMap).pixelSize/8/1024/1024/s);
StartTimer(timer);
for(iL=n;iL>0;iL--){
GetDevicePixelsQuickly(device,0,0,row,rowLength);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f ms GetPixelsQuickly(,,,,%d);// %d-bit pixels, %.3f MB/s\n",s*1e3
,(int)rowLength,(int)(**(**device).gdPMap).pixelSize
,(double)rowLength*(**(**device).gdPMap).pixelSize/8/1024/1024/s);
n*=100;
for(i=0;i<1;i++)if(row2[i]!=row[i])printf("Pixel %d: wrote %ld != read %ld\n"
,(int)i,row[i],row2[i]);
}
if(quickDraw>=gestalt32BitQD){
GWorldPtr aWorld,bWorld;
Rect r;
int error,pixelSize;
double pixels;
n=2;
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error)error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&aWorld->portRect,srcCopy,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopy,);// %dx%dx%d, %.3f MB/s\n"
,s*1e3,(int)r.right,(int)r.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
ShrinkRect(&r,20,1);
if(!error)error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&bWorld->portRect,srcCopy,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopy,);// %dx%d to %dx%dx%d, %.3f MB/s\n"
,s*1e3
,(int)aWorld->portRect.right,(int)aWorld->portRect.bottom
,(int)bWorld->portRect.right,(int)bWorld->portRect.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
ShrinkRect(&r,1,20);
if(!error)error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&bWorld->portRect,srcCopy,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
r=aWorld->portRect;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopy,);// %dx%d to %dx%dx%d, %.3f MB/s\n"
,s*1e3
,(int)aWorld->portRect.right,(int)aWorld->portRect.bottom
,(int)bWorld->portRect.right,(int)bWorld->portRect.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
n=100000;
}
if(quickDraw>=gestalt32BitQD){
GWorldPtr aWorld,bWorld;
Rect r;
int error,pixelSize;
double pixels;
n=2;
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error)error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&aWorld->portRect,srcCopyLiterally,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopyLiterally,);// %dx%dx%d, %.3f MB/s\n"
,s*1e3,(int)r.right,(int)r.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
ShrinkRect(&r,20,1);
if(!error)error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&bWorld->portRect,srcCopyLiterally,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopyLiterally,);// %dx%d to %dx%dx%d, %.3f MB/s\n"
,s*1e3
,(int)aWorld->portRect.right,(int)aWorld->portRect.bottom
,(int)bWorld->portRect.right,(int)bWorld->portRect.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
SetRect(&r,0,0,100,100);
pixels=(double)(r.right-r.left)*(r.bottom-r.top);
error=NewGWorld(&bWorld,8,&r,NULL,NULL,0);
ShrinkRect(&r,1,20);
if(!error)error=NewGWorld(&aWorld,8,&r,NULL,NULL,0);
if(!error){
LockPixels(GetGWorldPixMap(aWorld));
LockPixels(GetGWorldPixMap(bWorld));
StartTimer(timer);
for(iL=n;iL>0;iL--){
CopyWindows(aWorld,bWorld,&aWorld->portRect,&bWorld->portRect,srcCopyLiterally,NULL);
}
s=StopTimerSecs(timer)/n-overhead;
pixelSize=(**GetGWorldPixMap(aWorld)).pixelSize;
r=aWorld->portRect;
ffprintf(o,"%11.3f ms CopyWindows(,,,,srcCopyLiterally,);// %dx%d to %dx%dx%d, %.3f MB/s\n"
,s*1e3
,(int)aWorld->portRect.right,(int)aWorld->portRect.bottom
,(int)bWorld->portRect.right,(int)bWorld->portRect.bottom
,pixelSize,pixels*pixelSize/8/1024/1024/s);
}
DisposeGWorld(aWorld);
DisposeGWorld(bWorld);
n=100000;
}
if(1){ // time long arithmetic
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
jL=kL;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL; // long, register to register\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
jL=kL>>1;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL>>1;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
jL=kL+mL;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL+mL;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
jL=kL-mL;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL-mL;\n",s*1e6);
n/=10; /* all other operations take at least several microseconds */
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
jL=kL*mL;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL*mL;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
jL=kL/mL;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jL=kL/mL;\n",s*1e6);
n*=10;
}
if(1){ // time short arithmetic
register short jH,kH=1234,mH=5678;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
jH=kH;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH; // short, register to register\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
jH=kH>>1;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH>>1;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
jH=kH+mH;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH+mH;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
jH=kH-mH;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH-mH;\n",s*1e6);
n/=10; /* all other operations take at least several microseconds */
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
jH=kH*mH;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH*mH;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
jH=kH/mH;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs jH=kH/mH;\n",s*1e6);
n*=10;
}
n/=10; /* all other operations take at least several microseconds */
if(1){ // time double arithmetic
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=y;
x=y;
x=y;
x=y;
x=y;
x=y;
x=y;
x=y;
x=y;
x=y;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=y; // double\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
x=y+z;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=y+z;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
x=y-z;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=y-z;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
x=y*z;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=y*z;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
x=y/z;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=y/z;\n",s*1e6);
}
if(1){ // time transcendental functions
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
x=sin(y);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=sin(y);\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
x=sqrt(y);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=sqrt(y);\n",s*1e6);
n/=100;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
x=log(y);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=log(y);\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
x=exp(y);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=exp(y);\n",s*1e6);
n*=100;
}
o[1]=NULL; /* that's all we want to save in “TimeCPU results” */
if(1){ // time Fixed
yF=zF=0x12341234;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
xF=FixMul(yF,zF);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs xF=FixMul(yF,zF); // Fixed\n",s*1e6);
yF=(long)(PI*256);
zF=(long)(1.1*256);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
xF=FixDiv(yF,zF);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs xF=FixDiv(yF,zF);\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
xF=FixRatio(123,1234);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs xF=FixRatio(123,1234);\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
xF=DoubleToFix(y);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs xF=DoubleToFix(y);\n",s*1e6);
xF=(long)(PI*256);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
x=FixToDouble(xF);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs x=FixToDouble(xF);\n",s*1e6);
}
if(1){ // time rand()
n*=10;
for(;n>0;){
bufferHandle=NewHandle(n);
if(bufferHandle==NULL)bufferHandle=TempNewHandle(n,&osErr);
if(bufferHandle!=NULL)break;
n/=2;
printf("Reducing iterations to %ld to fit in available memory.\n",n);
}
assert(bufferHandle!=NULL);
HLockHi(bufferHandle);
buffer=*bufferHandle;
StartTimer(timer);
s=StopTimerSecs(timer);
StartTimer(timer);
RandFill(buffer,n);
s=StopTimerSecs(timer) - s;
DisposeHandle(bufferHandle);
buffer=NULL;
ffprintf(o,"%11.3f µs RandFill(,%ld); // i.e. %4.1f µs/byte\n"
,s*1e6,n,s*1e6/n);
n/=10;
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
i=randU();
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=randU(); // i.e. %4.1f µs/byte\n",s*1e6, s*1e6/2.);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
i=rand();
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=rand(); // i.e. %4.1f µs/byte\n",s*1e6, s*1e6/1.);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
i=Random();
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=Random();\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
i=nrand(127);
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=nrand(127);\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
i=127L*randU()>>16;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=127L*randU()>>16;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
i=127L*rand()>>15;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=127L*rand()>>15;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
i=randU()%(unsigned short)127;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=randU()%%(unsigned short)127;\n",s*1e6);
StartTimer(timer);
for(iL=n/10;iL>0;iL--){
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
i=rand()%127;
}
s=StopTimerSecs(timer)/n-overhead;
ffprintf(o,"%11.3f µs i=rand()%%127;\n",s*1e6);
}
DisposeTimer(timer);
fclose(dataFile); /* close “TimeCPU results” */
DrawMenuBar();
}